<template>
  <view>
    <view
      :class="
        'flex height100  box box-tb box-align-center ' +
        (!showScheduleOfDay ? '' : 'noShow')
      "
    >
      <scroll-view
        :scroll-y="true"
        v-if="showOneMonth"
        class="calendar pink-color box box-tb"
      >
        <!-- 表头的日期 -->
        <view
          class="horient top-handle box box-lr box-align-center box-pack-center"
        >
          <view
            @click="chooseYearAndMonth"
            class="date-area box box-lr box-align-center box-pack-center"
          >
            {{ cur_year }} 年 {{ cur_month }} 月
          </view>
        </view>

        <!-- 表头的周几 -->
        <view class="horient weeks box box-lr box-pack-center box-align-center">
          <view
            class="flex week"
            :data-idx="index"
            v-for="(item, index) in weeks_ch"
            :key="item.index"
          >
            {{ item }}
          </view>
        </view>

        <!-- 日期滑动显示区 -->
        <swiper
          :style="' height:' + swiperHeight + 'px'"
          duration="500"
          :current="currentId"
          :circular="true"
          @change="swiperChange"
        >
          <block v-for="(item, indexparent) in swiper_data" :key="indexparent">
            <swiper-item>
              <view
                class="days horient box box-lr box-wrap"
                style="height: 100%"
              >
                <!-- 渲染空格 -->
                <view
                  class="grid white-color box box-align-center box-pack-center"
                  :data-idx="indexparent"
                  v-for="item in swiper_empty[indexparent]"
                  :style="' height:' + lineHeight + 'px'"
                  :key="item.indexparent"
                >
                </view>

                <!-- 渲染非空格 -->
                <view
                  class="grid white-color box box-align-center box-pack-center"
                  :data-parentIdx="indexparent"
                  :data-childIdx="indexchild"
                  @click="tapDayItem"
                  v-for="(item, indexchild) in item"
                  :style="' height:' + lineHeight + 'px'"
                  :key="item.indexchild"
                >
                  <!-- 日期 -->
                  <view
                    :class="
                      'day ' +
                      (item.choosed ? 'border-radius pink-bg' : '') +
                      ' ' +
                      (item.day == CURDAY &&
                      cur_month == CURMONTH &&
                      CURYEAR == cur_year
                        ? 'text-red'
                        : '') +
                      ' box box-align-center box-pack-center'
                    "
                  >
                    {{ item.day }}</view
                  >
                  <!-- <view
                    :class="
                      'calendar-schedule-item ' +
                      (item.urgent == 1
                        ? 'normal'
                        : item.urgent == 2
                        ? 'middle'
                        : 'high')
                    "
                    v-for="(item, index) in scheduleShow(
                      swiper_schedules[indexparent][indexchild]
                    )"
                    :key="index"
                  > -->
                  <view
                    :class="
                      'calendar-schedule-item ' +
                      (item.urgent == 1
                        ? 'normal'
                        : item.urgent == 2
                        ? 'middle'
                        : 'high')
                    "
                    v-for="(item, index) in swiper_schedules[indexparent][
                      indexchild
                    ]"
                    v-if="index < 3"
                    :key="index"
                  >
                    <!-- <view class="calendar-schedule-item-dot">·</view> -->
                    <view
                      :class="
                        'calendar-schedule-item-text ' +
                        (item.t_isfinished == true ? ' task-finish-text' : ' ')
                      "
                    >
                      {{ item.t_title + ' ' + item.urgent }}
                      <!-- {{ item }} -->
                    </view>
                  </view>
                  <view
                    :class="'calendar-schedule-item '"
                    v-if="swiper_schedules[indexparent][indexchild].length > 3"
                  >
                    <view class="extraSchedule">
                      {{ swiper_schedules[indexparent][indexchild].length - 3 }}
                      条未显示
                    </view>
                  </view>

                  <!-- <view
                    :class="'calendar-schedule-item color-red'"
                    v-for="(item, index) in swiper_schedules[indexparent][
                      indexchild
                    ]"
                    :key="index"
                  >
                    <view class="calendar-schedule-item-dot">·</view>
                    <view class="calendar-schedule-item-text">
                      {{ item.t_title }}12321312312312312123412412
                    </view>
                  </view> -->
                  <!-- 
                  <view class="calendar-schedule-item color-gray">
                    <view :class="' calendar-schedule-item-dot '"> · </view>
                    <view class="calendar-schedule-item-text"> 开会 </view>
                  </view>

                  <view class="calendar-schedule-item">
                    <view
                      :class="
                        ' calendar-schedule-item-dot ' +
                        (true ? ' color-green' : 'color-red')
                      "
                    >
                      ·
                    </view>
                    <view class="calendar-schedule-item-text"> 开会 </view>
                  </view> -->
                </view>
              </view>
            </swiper-item>
          </block>
        </swiper>
      </scroll-view>
    </view>
    <!--弹出框的灰色背景,点击时关闭日期选择框-->
    <view
      class="drawer_screen"
      @click="powerDrawer"
      data-statu="close"
      v-if="showYearMonth"
    >
    </view>

    <!--弹出框-->
    <view v-if="showYearMonth" class="picker">
      <!-- 标题,当前选中的日期 -->
      <view class="picker-title">
        <view class="title-content">
          {{ choose_year }}年{{ choose_month }}月{{ choose_day }}日周{{
            choose_week
          }}
        </view>
      </view>

      <!-- 日期选择器 -->
      <view class="picker-content">
        <picker-view
          class="picker-view"
          indicator-style="height: 80rpx;"
          style="width: 100%; height: 240rpx"
          :value="picker_value"
          @change="pickerChange"
        >
          <picker-view-column>
            <view
              class="picker-view-left"
              style="line-height: 80rpx; height: 80rpx"
              v-for="(item, index) in picker_year"
              :key="index"
            >
              {{ item }}年
            </view>
          </picker-view-column>
          <picker-view-column>
            <view
              class="picker-view-center"
              style="line-height: 80rpx; height: 80rpx"
              v-for="(item, index) in picker_month"
              :key="index"
            >
              {{ item }}月
            </view>
          </picker-view-column>
          <picker-view-column>
            <view
              class="picker-view-right"
              style="line-height: 80rpx; height: 80rpx"
              v-for="(item, index) in picker_day"
              :key="index"
            >
              {{ item }}日
            </view>
          </picker-view-column>
        </picker-view>
      </view>

      <!-- 底部功能按钮 -->
      <view class="picker-bottom">
        <view class="bottom-button">
          <view class="bottom-today" @click="onTodayListener">今天</view>
          <view class="bottom-ok" @click="tapPickerBtn">确定</view>
        </view>
      </view>
    </view>

    <!-- 进入某一天的日程中右下角的添加按钮 -->
    <!-- <view v-if="showScheduleOfDay" class="com-widget-goTop">
      <image
        class="icon-gotop"
        @click="addNewScheduleListener"
        :src="imageLists.ic_add"
      ></image>
    </view> -->

    <!-- 选中日期的日程安排表 -->
    <view :class="' sd-container ' + (showScheduleOfDay ? '' : ' noShow')">
      <!-- 头部 -->
      <view class="sd-head">
        <view class="sd-head-top">
          <view class="sd-ht-center" @click="onDateListener">
            <view class="sd-head-YM">
              {{ chooseYear }}年{{ chooseMonth }}月
            </view>
            <view class="sd-head-arrowdown">
              <image
                class="sd-head-arrowdown-img"
                :src="imageLists.ic_arrowdown"
              ></image>
            </view>
          </view>
        </view>
        <view class="sd-head-DW">
          <view class="sd-head-DW-right">
            {{ chooseDay }}日.周{{ chooseWeek }}
          </view>
          <view class="sd-head-DW-left">
            -----------------------------------------------
          </view>
        </view>
      </view>

      <!-- 内容 -->
      <view class="sd-center">
        <!-- 无日程安排的背景图 -->
        <view v-if="!hasScheduleDay">
          <block name="defaultView">
            <view class="template-N-view">
              <image
                class="template-N-img"
                :src="imageLists.pic_default"
              ></image>
            </view>
          </block>
        </view>

        <!-- 日程安排列表 -->
        <view
          class="sd-center-item"
          v-for="(item, index) in schedule_day_arrays"
          :key="index"
        >
          <!-- 事件未完成时根据紧急程度显示不同颜色 -->
          <view
            v-if="item.t_isfinished != 1"
            :class="
              ' sd-center-item-hot ' +
              (item.t_isfinished == 1
                ? 'sd-color-gray'
                : item.urgent == 1
                ? 'sd-color-normal'
                : item.urgent == 2
                ? 'sd-color-middle'
                : 'sd-color-high')
            "
            @click="changeTodoFinishState(item, true)"
            :data-index="index"
          >
            •
          </view>

          <!-- 事件完成时显示对号 -->
          <view
            v-if="item.t_isfinished == 1"
            class="sd-center-item-hot"
            @click="changeTodoFinishState(item, false)"
            :data-index="index"
          >
            <image class="sd-center-item-img" :src="imageLists.ic_done"></image>
          </view>

          <!-- 事件标题 -->
          <view
            :class="
              ' sd-center-item-time ' +
              (item.t_isfinished == 1 ? ' sd-color-gray1' : '')
            "
            @click="goToDetail(item)"
            :data-index="index"
          >
            {{ item.t_title }}
          </view>

          <!-- 时间具体内容 -->
          <view
            :class="
              ' sd-center-item-content ' +
              (item.t_isfinished == 1 ? ' sd-color-gray1' : '')
            "
            @click="goToDetail(item)"
            :data-index="index"
          >
            {{ item.t_desc == null ? '' : item.t_desc }}
          </view>

          <!-- 具体时间 -->
          <view
            :class="
              ' sd-center-item-use ' +
              (item.t_isfinished == 1 ? ' sd-color-gray1' : '')
            "
            @click="goToDetail(item)"
            :data-index="index"
          >
            {{ getTime(item.t_time) }}
          </view>
        </view>
      </view>
    </view>

    <!-- 某个日程安排的详情框 -->
    <van-popup
      :show="popupShow"
      position="bottom"
      custom-style="height: 80%;"
      @close="popupClose"
      round
    >
      <add-todo
        v-if="popupShow"
        :todoFromParent="todoDetail"
        :state="2"
        @addFinish="popupClose"
      ></add-todo>
    </van-popup>
  </view>
</template>

<script>
var app = getApp();
import { addTodo } from '../addTodo/addTodo';

var utils = require('@/static/todo/utils.js');

var top_current = 1;
export default {
  props: ['height'],
  components: { addTodo },
  data() {
    return {
      showOneMonth: true,
      swiper_data: [[], [], []], //二维数组,当前以及相邻月份的天数
      swiper_empty: [[], [], []], //二维数组,当前以及相邻月份的空格数
      swiper_schedules: [[], [], []], //二维数组,当前以及相邻月份的安排
      days: {}, //当前选中月份的天数
      childIdx: {}, //当前选中的日期对应的下标
      showYearMonth: false,
      currentId: 1, //当前面板对应的id

      swiperHeight: 0,
      CURDAY: {}, //当前日期
      CURMONTH: {}, //当前月份
      CURYEAR: {}, //当前年份
      showScheduleOfDay: false,
      choose_year: {}, //当前选中的年份
      choose_month: {}, //当前选中的月份
      choose_day: {}, //当前选中的日期
      choose_hours: {}, //当前选中的小时
      choose_minutes: {}, //当前选中的分钟
      choose_week: {}, //当前选中的周
      cur_year: '--', //当前年份
      cur_month: '--', //当前月份
      cur_day: '--',
      cur_hours: '',
      cur_minutes: '',
      cur_weeks: {},
      weeks_ch: {}, //上侧显示的中文周几
      isClickToday: false,

      showPicker: false,
      parentIdx: '',
      chooseYear: {},
      chooseMonth: {},
      chooseDay: {},
      chooseWeek: {},
      dateString: '',
      picker_value: [], //日期选择器选中的值
      picker_year: {}, //日期选择器中的年份
      picker_month: {}, //日期选择器中的月份
      picker_day: {}, //日期选择器中的日期,需要根据选中的年份和月份不断变化
      schedule_day_arrays: [], //当日日程安排列表
      hasScheduleDay: '', //点击的日期是否有日程安排
      popupShow: false, // 日程安排的详细情况
      todoDetail: {},

      imageLists: {
        ic_search: '/static/todo/images/ic_search.png',
        ic_add: '/static/todo/images/ic_add.png',
        ic_arrowdown: '/static/todo/images/ic_arrowdown.png',
        pic_default: '/static/todo/images/pic_default.png',
        ic_done: '/static/todo/images/ic_done.png',
      },
      lineHeight: 0,
    };
  },

  async created() {
    top_current = 1;
    this.swiperHeight = this.height - 66;

    var date = new Date();
    var CURDAY = date.getDate();
    var CURMONTH = date.getMonth() + 1;
    var CURYEAR = date.getFullYear();
    await this.initData(date);
    this.setData({
      CURDAY: CURDAY,
      CURMONTH: CURMONTH,
      CURYEAR: CURYEAR,
    });
    var dateString = this.dateString;

    if (dateString) {
      await this.querySchedule(dateString);
    }

    console.log('swiper_data');
    console.log(this.swiper_data);
    var lines = Math.ceil(
      (this.swiper_empty[this.currentId].length +
        this.swiper_data[this.currentId].length) /
        7
    );
    this.lineHeight = this.swiperHeight / lines;
    console.log(this.swiperHeight, this.lineHeight);
  },
  methods: {
    /**
     * 生命周期函数--监听页面显示
     */
    async onShowClone3389() {
      this.setData({
        showScheduleOfDay: false,
      });
      await this.oneMonthSchedule(this.cur_year, this.cur_month, top_current);
      // var info = uni.getSystemInfoSync();
      // console.log(info);
      // var screenWidth = info.screenWidth;
      // var pixelRatio = 750 / screenWidth;
      // var windowHeight = info.windowHeight;
      // var screenHeight = info.screenHeight;
      // var height = windowHeight - 65 - 50;
      // var screenWidth = app.data.deviceInfo.screenWidth;
      // // var pixelRatio = 750 / screenWidth;
      // // var windowHeight = app.data.deviceInfo.windowHeight;
      // // var screenHeight = app.data.deviceInfo.screenHeight;
      // // console.log(pixelRatio + "屏幕高度：" + windowHeight + "ddd:" + screenHeight);
      // // var height = windowHeight - 65;
      // // console.log(pixelRatio + "屏幕高度：" + height + "ddd:" + screenHeight);
      // var date = new Date()
      // var CURDAY = date.getDate();
      // this.initData(date);
      // this.setData({
      //   height: height,
      //   CURDAY: CURDAY,
      // })

      var dateString = this.dateString;

      if (dateString) {
        await this.querySchedule(dateString);
      }
    },

    /**
     * 日历初始化
     */
    async initData(date) {
      // console.log(date);
      const cur_year = date.getFullYear();
      const cur_month = date.getMonth() + 1;
      const cur_hours = date.getHours() + 1;
      const cur_day = date.getDate();
      const cur_minutes = date.getMinutes();
      const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
      const cur_weeks = this.getCurrentWeeks(date);

      // 获得当前月份左滑右滑对应的月份和年份,并初始化数据
      var bottom_month = cur_month;
      var bootom_year = cur_year;
      var top_month = cur_month;
      var top_year = cur_year;
      console.log(cur_year + ':' + cur_month + ':' + cur_day);

      if (cur_month == 1) {
        bottom_month = 12;
        bootom_year--;
      } else {
        bottom_month--;
      }

      if (cur_month == 12) {
        top_month = 1;
        top_year++;
      } else {
        top_month++;
      }

      await this.calculateEmptyGrids(bootom_year, bottom_month, 0);
      await this.calculateDays(bootom_year, bottom_month, 0);
      await this.calculateEmptyGrids(cur_year, cur_month, 1);
      await this.calculateDays(cur_year, cur_month, 1);
      await this.calculateEmptyGrids(top_year, top_month, 2);
      await this.calculateDays(top_year, top_month, 2);

      this.setData({
        choose_year: cur_year,
        choose_month: cur_month,
        choose_day: cur_day,
        choose_hours: cur_hours,
        choose_minutes: cur_minutes,
        choose_week: cur_weeks,
        cur_year,
        cur_month,
        childIdx: cur_day - 1,
        cur_day: cur_day,
        cur_hours,
        cur_minutes,
        cur_weeks,
        weeks_ch,
        currentId: 1,
      });
      this.currentId = 1;

      this.initDayItem(); // this.chooseYearAndMonth();
    },

    /**
     * 滑动切换日历事件
     */

    /**
     * 预加载下个月的数据
     * Type:0 表示下一个月   1表示上一月
     */
    async prestrainNextData(cur_year, cur_month, Type, next_current) {
      if (Type == 0) {
        if (cur_month == 12) {
          cur_month = 1;
          cur_year++;
        } else {
          cur_month++;
        }
      } else {
        if (cur_month < 2) {
          cur_month = 12;
          cur_year--;
        } else {
          cur_month--;
        }
      }

      console.log(
        '当前预加载：' +
          cur_year +
          '月' +
          cur_month +
          '日' +
          next_current +
          '下标'
      );
      this.calculateEmptyGrids(cur_year, cur_month, next_current);
      await this.calculateDays(cur_year, cur_month, next_current);
    },

    /**
     * 滑动切换日历,同时需要预加载相邻月份的信息
     */
    async swiperChange(e) {
      console.log(e);
      var cur_year = this.cur_year;
      var cur_month = this.cur_month;
      var new_current = e.detail.current;
      var next_current;
      console.log(new_current); // console.log(e);

      if (this.isClickToday) {
        this.setData({
          isClickToday: false,
        });
      } else {
        console.log(new_current + '<---' + top_current);

        if (new_current == 2) {
          if (top_current == 0) {
            //右滑  月份减一
            next_current = 1;

            if (cur_month < 2) {
              cur_month = 12;
              cur_year--;
            } else {
              cur_month--;
            }

            await this.prestrainNextData(cur_year, cur_month, 1, next_current);
          } else {
            //左滑  月份加一
            next_current = 0;

            if (cur_month == 12) {
              cur_month = 1;
              cur_year++;
            } else {
              cur_month++;
            }

            await this.prestrainNextData(cur_year, cur_month, 0, next_current);
          }
        } else {
          if (new_current == 1) {
            if (top_current == 2) {
              //右滑  月份减一
              next_current = 0;

              if (cur_month < 2) {
                cur_month = 12;
                cur_year--;
              } else {
                cur_month--;
              }

              await this.prestrainNextData(
                cur_year,
                cur_month,
                1,
                next_current
              );
            } else {
              //左滑  月份加一
              next_current = 2;

              if (cur_month == 12) {
                cur_month = 1;
                cur_year++;
              } else {
                cur_month++;
              }

              await this.prestrainNextData(
                cur_year,
                cur_month,
                0,
                next_current
              );
            }
          } else {
            if (top_current == 1) {
              //右滑  月份减一
              next_current = 2;

              if (cur_month < 2) {
                cur_month = 12;
                cur_year--;
              } else {
                cur_month--;
              }

              await this.prestrainNextData(
                cur_year,
                cur_month,
                1,
                next_current
              );
            } else {
              //左滑  月份加一
              next_current = 1;

              if (cur_month == 12) {
                cur_month = 1;
                cur_year++;
              } else {
                cur_month++;
              }

              await this.prestrainNextData(
                cur_year,
                cur_month,
                0,
                next_current
              );
            }
          }
        }

        console.log('currentId：' + new_current);
        top_current = new_current;
        this.setData({
          cur_year: cur_year,
          cur_month: cur_month,
          currentId: new_current,
        });
      }
    },

    /**
     * 日期设置
     */

    /**
     * 获取当前  星期几
     */
    getCurrentWeeks(date) {
      const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
      var weeks_number = date.getDay(); // console.log("数字  星期" + weeks_number);

      return weeks_ch[weeks_number];
    },

    /**
     *  计算每月有多少天
     */
    getThisMonthDays(year, month) {
      return new Date(year, month, 0).getDate();
    },

    /**
     * 计算每月第一天是星期几
     */
    getFirstDayOfWeek(year, month) {
      return new Date(Date.UTC(year, month - 1, 1)).getDay();
    },

    /**
     * 计算在每月第一天在当月第一周之前的空余的天数
     */
    async calculateEmptyGrids(year, month, currentTag) {
      const firstDayOfWeek = this.getFirstDayOfWeek(year, month); //计算每月第一天是星期几
      var swiper_empty = this.swiper_empty;
      let emptyGrids = [];

      if (firstDayOfWeek > 0) {
        // 不是每周的第一天,即不是周日
        // 添加空格
        for (let i = 0; i < firstDayOfWeek; i++) {
          emptyGrids.push(i);
        }

        swiper_empty[currentTag] = emptyGrids;
        this.setData({
          swiper_empty,
        });
      } else {
        //第一天是周日,代表前面没有空格
        swiper_empty[currentTag] = [];
        this.setData({
          swiper_empty,
        });
      }
    },

    /**
     * 渲染日历格子
     */
    async calculateDays(year, month, currentTag) {
      await this.oneMonthSchedule(year, month, currentTag); //获取该月的日程信息
      let days = [];
      const thisMonthDays = this.getThisMonthDays(year, month); //计算每月有多少天

      for (let i = 1; i <= thisMonthDays; i++) {
        days.push({
          day: i,
          choosed: false,
        });
      }

      var swiper_data = this.swiper_data;
      swiper_data[currentTag] = days;
      this.setData({
        days,
        swiper_data: swiper_data,
      });
    },

    /**
     * 获取某月的日程信息
     */
    async oneMonthSchedule(year, month, currentTag) {
      // todo:请求当月所有代办
      const res = await this.hjRequest.run('findTodoByDate', {
        year: year,
        month: month,
        type: 2,
      });
      var todoList = res.result;
      var schedule = [];
      var thisMonthDays = this.getThisMonthDays(year, month); //本月的天数
      var index = 0;
      for (let i = 0; i < thisMonthDays; i++) {
        schedule.push([]);

        while (index < todoList.length) {
          var currentDay = new Date(todoList[index].t_time);
          if (currentDay.getDate() == i + 1) {
            schedule[i].push(todoList[index]);
            index++;
          } else {
            break;
          }
        }
      }
      this.swiper_schedules[currentTag] = schedule;
      this.swiper_schedules = this.swiper_schedules.slice(0);
      console.log(this.swiper_schedules);

      // this.swiper_schedules[currentTag]=res.result
      // console.log(this.swiper_schedules)
    },

    // 递增、递减切换月份
    async handleCalendar(e) {
      const handle = e.currentTarget.dataset.handle;
      const cur_year = this.cur_year;
      const cur_month = this.cur_month;

      if (handle === 'prev') {
        let newMonth = cur_month - 1;
        let newYear = cur_year;

        if (newMonth < 1) {
          newYear = cur_year - 1;
          newMonth = 12;
        }

        await this.calculateDays(newYear, newMonth);
        this.calculateEmptyGrids(newYear, newMonth);
        this.setData({
          showPicker: false,
          cur_year: newYear,
          cur_month: newMonth,
        });
      } else {
        let newMonth = cur_month + 1;
        let newYear = cur_year;

        if (newMonth > 12) {
          newYear = cur_year + 1;
          newMonth = 1;
        }

        await this.calculateDays(newYear, newMonth);
        this.calculateEmptyGrids(newYear, newMonth);
        this.setData({
          showPicker: false,
          cur_year: newYear,
          cur_month: newMonth,
        });
      }
    },

    /**
     * 初始默认点击
     */
    initDayItem() {
      var that = this;
      const days = that.swiper_data[top_current];

      if (this.childIdx) {
        var idx = that.childIdx;
      } else {
        var idx = that.cur_day - 1;
      }

      days[idx].choosed = !days[idx].choosed; //设置当前日期默认选中

      var swiper_data = this.swiper_data;
      swiper_data[top_current] = days;
      this.setData({
        days: days,
        swiper_data: swiper_data,
      });
    },

    // 点击日历上某一天,跳转到当日日程安排
    async tapDayItem(e) {
      // console.log(e)
      var parentIdx = e.currentTarget.dataset.parentidx;
      const childIdx = e.currentTarget.dataset.childidx;
      const days = this.swiper_data[parentIdx];
      console.log(days[childIdx]);

      for (var i = 0; i < days.length; i++) {
        days[i].choosed = false;
      }

      const cur_hours = this.cur_hours;
      const cur_minutes = this.cur_minutes; //创建 时  分

      const picker_value_time = [];
      let picker_hours = [];
      let picker_minutes = [];

      for (let i = 1; i <= 24; i++) {
        picker_hours.push(i);
      }

      for (let i = 0; i <= 59; i++) {
        picker_minutes.push(i);
      }

      const idx_hours = picker_hours.indexOf(cur_hours);
      const idx_minutes = picker_minutes.indexOf(cur_minutes);
      var cur_day = childIdx + 1;
      const date = new Date(
        Date.UTC(this.cur_year, this.cur_month - 1, cur_day)
      );
      console.log(
        '当前' + this.cur_year + ':' + this.cur_month + ':' + cur_day
      );

      var cur_weeks = this.getCurrentWeeks(date);
      days[childIdx].choosed = !days[childIdx].choosed;
      var swiper_data = this.swiper_data; // console.log("top_current" + top_current);

      swiper_data[top_current] = days;
      this.setData({
        cur_day: cur_day,
        cur_weeks: cur_weeks,
        swiper_data: swiper_data,
        childIdx: childIdx,
        days,
      });
      var chooseYear = date.getFullYear();
      var chooseMonth = date.getMonth() + 1;
      var chooseDay = date.getDate();
      var chooseWeek = this.getCurrentWeeks(date);
      this.setData({
        parentIdx,
        childIdx,
        chooseYear,
        chooseMonth,
        chooseDay,
        chooseWeek,
        showScheduleOfDay: true,
      });
      var dateString = utils.transformDate2(chooseYear, chooseMonth, chooseDay); //2021-11-11
      this.setData({
        dateString,
      });
      await this.querySchedule(dateString);
    },

    // 点击顶部年月调用picker选择器
    chooseYearAndMonth() {
      const cur_year = this.cur_year;
      const cur_month = this.cur_month;
      var cur_day = this.cur_day;
      const cur_weeks = this.cur_weeks;
      console.log(
        'cur_year:' + cur_year + 'cur_month' + cur_month + 'cur_day:' + cur_day
      );
      let picker_year = [];
      let picker_day = [];
      let picker_month = [];

      for (let i = 1900; i <= 2100; i++) {
        picker_year.push(i);
      }

      for (let i = 1; i <= 12; i++) {
        picker_month.push(i);
      }

      var thisMonthDays = this.getThisMonthDays(cur_year, cur_month);

      if (cur_day > thisMonthDays) {
        cur_day = 1;
      }

      for (let i = 1; i <= thisMonthDays; i++) {
        picker_day.push(i);
      }

      const idx_year = picker_year.indexOf(cur_year);
      const idx_month = picker_month.indexOf(cur_month);
      const idx_day = picker_day.indexOf(cur_day);

      this.setData({
        picker_value: [idx_year, idx_month, idx_day],
        choose_year: cur_year,
        choose_month: cur_month,
        choose_day: cur_day,
        choose_week: cur_weeks,
        picker_year,
        picker_day,
        picker_month,
        showYearMonth: true,
      });
    },

    // 当picker选择器值改变时,修改选中的值,同时修改每月天数
    pickerChange(e) {
      console.log(e);
      var that = this;
      const val = e.detail.value;
      var picker_day = [];
      // 更改滚动选择器天数
      var choose_year = this.picker_year[val[0]];
      var choose_month = this.picker_month[val[1]];
      var choose_day = this.picker_day[val[2]];

      if (
        this.choose_month != this.picker_month[val[1]] ||
        this.picker_year[val[0]] != this.choose_year
      ) {
        // 当月份或者年份与当前展示的月份不一样时需要更新天数
        var thisMonthDays = this.getThisMonthDays(choose_year, choose_month);
        console.log(thisMonthDays + '');

        for (let i = 1; i <= thisMonthDays; i++) {
          picker_day.push(i);
        }

        that.setData({
          picker_day: picker_day,
        });
      }

      const date = new Date(
        Date.UTC(choose_year, choose_month - 1, choose_day)
      );
      var choose_week = this.getCurrentWeeks(date);

      this.setData({
        choose_year,
        choose_month,
        choose_day,
        choose_week,
      });
    },

    // 确定picker日期弹框选择器选择结果
    tapPickerBtn(e) {
      var that = this;
      var isClickToday = false;

      if (top_current != 1) {
        isClickToday = true;
      }

      top_current = 1; //由于需要重新请求数据,所以设置当前选中的页面是中间的

      const o = {
        showYearMonth: false,
        cur_year: that.choose_year,
        cur_month: that.choose_month,
        cur_day: that.choose_day,
        cur_weeks: that.choose_week,
      };
      this.setData({
        swiper_schedules: [],
        swiper_data: [],
        swiper_empty: [],
        isClickToday,
      });
      var date = new Date(
        that.choose_year,
        that.choose_month - 1,
        that.choose_day
      );
      this.setData(o);

      this.initData(date); //初始化日历
    },

    /**
     * 弹出框中点击今天按钮对应的回调函数
     */
    onTodayListener(e) {
      var isClickToday = false;
      var date = new Date();
      const cur_year = date.getFullYear();
      const cur_month = date.getMonth() + 1;

      const cur_day = date.getDate();
      const cur_weeks = this.getCurrentWeeks(date);

      var thisMonthDays = this.getThisMonthDays(cur_year, cur_month);
      var picker_day = [];

      for (let i = 1; i <= thisMonthDays; i++) {
        picker_day.push(i);
      }

      this.setData({
        picker_day: picker_day,
      });
      const idx_year = this.picker_year.indexOf(cur_year);
      const idx_month = this.picker_month.indexOf(cur_month);
      const idx_day = picker_day.indexOf(cur_day);

      this.setData({
        isClickToday,
        picker_value: [idx_year, idx_month, idx_day],
        choose_year: cur_year,
        choose_month: cur_month,
        choose_day: cur_day,
        choose_hours: this.cur_hours,
        choose_minutes: this.cur_minutes,
        choose_week: cur_weeks,
      });
    },

    /**
     * 点击灰色背景,关闭日期选择弹窗
     */
    powerDrawer(e) {
      this.setData({
        showYearMonth: false,
      });
    },

    /**
     * 某天日程界面JS
     */

    /**
     * 点击某天
     * 查询某天的日程
     */
    async querySchedule(dateString) {
      // todo:查找当天日程
      console.log(dateString);
      var date = new Date(dateString);
      var day = date.getDate();
      var month = date.getMonth() + 1;
      var year = date.getFullYear();
      console.log(date);
      const res = await this.hjRequest.run('findTodoByDate', {
        day,
        year,
        month,
        type: 1,
      });
      console.log(res);
      if (res.result.length != 0) {
        this.hasScheduleDay = true;
      } else {
        this.hasScheduleDay = false;
      }
      this.schedule_day_arrays = res.result;
    },

    /**
     * 时间点击事件,切换到日历展示界面
     */
    onDateListener(e) {
      this.onShowClone3389({});
      this.setData({
        showScheduleOfDay: false,
      });
    },

    /**
     * 标记完成
     */
    onFinishOnLister(e) {
      // todo:设置时间状态
      console.log(e);
    },

    /**
     * 日程点击事件
     */
    onScheduleItemListener(e) {
      var that = this;
      var index = e.currentTarget.dataset.index;
      var schedule_day_arrays = this.schedule_day_arrays;
      var item = schedule_day_arrays[index];
      console.log(item);
      var isUrgent = item.isUrgent; //isUrgent==2    为生日时：显示弹出框

      var scheduleId = item.scheduleId;
      uni.navigateTo({
        url: '../schedule-detail/schedule-detail?scheduleId=' + scheduleId,
      });
      that.setData({
        isNavigateTo: true,
      });
    },
    async changeTodoFinishState(item, finishState) {
      console.log(item);
      const res = await this.hjRequest.run('setTodoState', {
        todo_id: item._id,
        finished: finishState,
      });
      if (res.result.updatedCount == 1) {
        //修改成功
        item.t_isfinished = finishState;
        wx.showToast({
          title: `修改成功`,
          duration: 500,
        });
      }

      console.log(res);
    },
    async goToDetail(item) {
      // 跳转到详情页
      console.log(item);
      const res = await this.hjRequest.run('findTodoBoard', { _id: item._id });
      console.log(res);
      item.t_board = {
        board_id: res.result.board_id._id,
        board_title: res.result.board_id.b_title,
      };
      this.todoDetail = item;
      this.popupShow = true;
    },
    async popupClose() {
      await this.querySchedule(this.dateString);

      this.popupShow = false;
    },
    getTime(time) {
      var date = new Date(time);
      var retStr = date.getHours() + ':' + date.getMinutes();

      return retStr;
    },
  },
  computed: {
    // scheduleShow(schedule) {
    //   return (schedule) => {
    //     if (schedule.length < 3) {
    //       return schedule;
    //     } else {
    //       var ret = [];
    //       ret.push(schedule[0]);
    //       ret.push(schedule[1]);
    //       ret.push(schedule[2]);
    //       console.log(ret[0]);
    //       return ret;
    //     }
    //   };
    // },
    // aaa(event, schedule) {
    //   console.log(schedule);
    //   return schedule.filter((item, index) => {
    //     return index < 3;
    //   });
    // },
  },
};
</script>
<style>
@import './calendar.css';
</style>
